#ifndef get_data_cpp
#define get_data_cpp

#include "dht11.h"
#include<string>
#include<iostream>
#include<fstream>
#include<wiringPi.h>

dht11 DHT11;
#define DHT11PIN 18
int illuPin = 23;
float illuVal = 0;
float temp;
float hum;
using namespace std;

string formatData(float t, float h, float i) {
  string data = to_string(t) + "," + to_string(h) + "," + to_string(i);
  return data;
}

void saveDataToLocal(string data) {
    ofstream file("../data.txt");
    if (file.is_open()) {
        file << data;
        file.close();
        cout << "数据 " << data <<  " 已保存到本地" << endl;
    } else {
        cout << "数据保存失败！" << endl;
    }
}

void loop() {
    while (1) {
        if (DHT11.read(DHT11PIN) == DHTLIB_OK) { //温湿度传感器正常工作
            temp = (float)DHT11.temperature;
            hum = (float)DHT11.humidity;
        } else {
            temp = 0;
            hum = 0;
        }
        illuVal = analogRead(illuPin);  //获取光照强度
        saveDataToLocal(formatData(temp, hum, illuVal));
        delay(1000); //每隔1000ms保存一次
    }
}

int main() {
    loop();
    return 0;
}


#endif